package com.ikangtai.shecare.main;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.baseView.NoScrollViewPager;
import com.ikangtai.shecare.common.eventBusMsg.BBTLineChartDataMsg;
import com.ikangtai.shecare.common.eventBusMsg.ChooseStateMsg;
import com.ikangtai.shecare.common.eventBusMsg.CurveMsg;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.ButtonUtil;
import com.ikangtai.shecare.common.util.DisplayUtil;
import com.ikangtai.shecare.curve.CurveHelpActivity;
import com.ikangtai.shecare.curve.SaveChartImg2Local;
import com.ikangtai.shecare.curve.mpchart.BBTLineChart;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subcriber;


public class HealthFragment extends Fragment {
    //private WebView webview;
    private ImageView topBar_back, share, curveHelp;
    private LinearLayout main_lin;
    private ChangeColorIconWithTextView three;
    private NoScrollViewPager mViewPager;
    private SaveChartImg2Local saveChartImg2Local;
    private String buildType;

    private BBTLineChart mBBTChart;


    public HealthFragment() {
        // Required empty public constructor
        EventBus.getDefault().register(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        AppLog.i("LifeCycle get in HealthFragment onAttach!");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppLog.i("LifeCycle get in HealthFragment onCreate!");
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        AppLog.i("LifeCycle get in HealthFragment onViewCreated!");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        AppLog.i("LifeCycle get in HealthFragment onActivityCreated!");
    }

    @Override
    public void onStart() {
        super.onStart();
        AppLog.i("LifeCycle get in HealthFragment onStart!");
    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        AppLog.i("LifeCycle get in HealthFragment onResume!");
//        EventBus.getDefault().post(new HomeFragmentVisibleMsg());
//    }

    @Override
    public void onPause() {
        super.onPause();
        AppLog.i("LifeCycle get in HealthFragment onPause!");
    }

    @Override
    public void onStop() {
        super.onStop();
        AppLog.i("LifeCycle get in HealthFragment onStop!");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        AppLog.i("LifeCycle get in HealthFragment onDestroyView!");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        AppLog.i("LifeCycle get in HealthFragment onDetach!");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        AppLog.i("LifeCycle get in HealthFragment onCreateView!");

        EventBus.getDefault().register(this);
        View view = inflater.inflate(R.layout.fragment_health, container, false);
        initView(view);

        return view;
    }

    private final void initView(View view) {
//        webview = (WebView) view.findViewById(R.id.webview);
//        WebViewCfg webViewCfg = new WebViewCfg(webview);
//        webview = webViewCfg.getCfgedWebView(getActivity());
//
//        loadWebview();


        topBar_back = (ImageView) view.findViewById(R.id.topBar_back);
        topBar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity mActivity = (Activity) v.getContext();

                main_lin = (LinearLayout) mActivity.findViewById(R.id.main_lin);
                three = (ChangeColorIconWithTextView) mActivity.findViewById(R.id.id_indicator_three);
                main_lin.setVisibility(View.VISIBLE);
                three.setVisibility(View.VISIBLE);

                mViewPager = (NoScrollViewPager) mActivity.findViewById(R.id.fragmentContent);
                mViewPager.setCurrentItem(0, false);
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                DisplayUtil.setFullScreen(mActivity, false);
            }
        });

        share = (ImageView) view.findViewById(R.id.share);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ButtonUtil.isFastDoubleClick(R.id.share)) { // 按钮点击次数控制
                    return;
                } else {
                    AppLog.i("get User Msg And Draw Table");
                    Toast.makeText(v.getContext(), R.string.BBTToast, Toast.LENGTH_SHORT).show();
                    saveChartImg2Local = new SaveChartImg2Local();
                    saveChartImg2Local.getUserMsgAndDrawTable(v.getContext());
                }
            }
        });

        curveHelp = (ImageView) view.findViewById(R.id.curve_help);
        curveHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    startActivity(new Intent(getActivity(), CurveHelpActivity.class));
            }
        });
        mBBTChart = (BBTLineChart) view.findViewById(R.id.bbtChart);

        if(App.dayUnitDSOutputsList.size() > 0) {
            mBBTChart.init(getActivity());
            drawBBTChart();
        }
    }

    @Subcriber
    public void onCurveMsg(CurveMsg curveMsg) {
        AppLog.i("HealthFragment received CurveMsg!");
        //webview.loadUrl("file:///android_asset/health/bbtChart.html");
        if(App.dayUnitDSOutputsList.size() > 0) {
            mBBTChart.init(getActivity());
            drawBBTChart();
        }
    }

    @Subcriber
    private void changeState(ChooseStateMsg mState) {
        AppLog.i("HealthFragment received ChooseStateMsg!");
        if(App.dayUnitDSOutputsList.size() > 0) {
            mBBTChart.initBBTLegend();
            drawBBTChart();
        }
    }

//    private void loadWebview() {
//        AppLog.i("huxiao loadWebview in HealthFragment");
//        webview.loadUrl("file:///android_asset/health/bbtChart.html");
//    }

    private void drawBBTChart() {
        AppLog.i("HealthFragment drawBBTChart!");
        new Thread(new Runnable() {
            @Override
            public void run() {
                mBBTChart.setBBTData();
                EventBus.getDefault().post(new BBTLineChartDataMsg());
            }
        }).start();
    }

    @Subcriber
    public void onBBTLineChartDataMsg(BBTLineChartDataMsg bbtLineChartDataMsg) {
        AppLog.i("HealthFragment received BBTLineChartDataMsg!");
        mBBTChart.drawBBTChart();
        AppLog.i("HealthFragment received BBTLineChartDataMsg end! mBBTChart.getLowestVisibleX() = " + mBBTChart.getLowestVisibleX());
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        AppLog.i("LifeCycle get in HealthFragment setUserVisibleHint!");
        if (isVisibleToUser) {
            //StatusBarUtil.setTranslucent(getActivity());
            AppLog.i("LifeCycle get in HealthFragment setUserVisibleHint isVisibleToUser!");
            // ！这是保证进去之后滚动到 LEFT_EDGE_XAXIS_DAY 日期的关键！因为滚动必须在图所在的View可见的情况下奏效！
            // 之前一次性加载不可见时滚动无效，后期再进入HealthFragment不重走任何生命周期，因此没有机会滚动。
            // 所以在这里判断可见的情况下，让其滚动。
            // 等100ms的原因是必须完全可见，有些较慢机型必须如此。
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(App.dayUnitDSOutputsList.size() > 0) {
                        mBBTChart.moveViewToX(BBTLineChart.LEFT_EDGE_XAXIS_DAY);
                    }
                }
            }, 100);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
